package com.founder.android.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.founder.android.cache.AsyncImageRequest;
import com.founder.android.data.WeatherCondition;
import com.founder.android.data.WeatherInfo;
import com.founder.android.parser.WeatherXMLParserHandler;
import com.founder.android.sample.R;

/**
 * 天气数据list显示适配器
 * @author chen_weihua
 *
 */
public class WeatherAdapter extends BaseExpandableListAdapter {
	
	private List<WeatherInfo> mWeatherData;
	
    private LayoutInflater mInflater;
    
    private Context mContext;
    
    /**
     * 初始化方法
     * @param context
     */
    public WeatherAdapter(Context context) {
    	mWeatherData = new ArrayList<WeatherInfo>();
    	mInflater = LayoutInflater.from(context);
    	mContext = context;
    }

    /**
     * 添加一个数据
     * @param weatherData
     */
	public synchronized void addData(WeatherInfo weatherData) {
		mWeatherData.add(weatherData);
		notifyDataSetChanged();
	}
	
    /**
     * 删除一个数据
     * @param weatherData
     */
	public synchronized void removeData(WeatherInfo weatherData) {
		mWeatherData.remove(weatherData);
		notifyDataSetChanged();
	}
	
	/**
	 * 添加一批数据
	 * @param datas
	 */
	public synchronized void addDatas(List<WeatherInfo> datas) {
		mWeatherData.addAll(datas);
		notifyDataSetChanged();
	}
	
	/**
	 * 刷新数据
	 * @param datas
	 */
	public synchronized void setDatas(List<WeatherInfo> datas) {
		mWeatherData.clear();
		mWeatherData.addAll(datas);
		notifyDataSetChanged();
	}
	
	/**
	 * 取得当前数据
	 * @return
	 */
	public List<WeatherInfo> getDatas() {
		return mWeatherData;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		WeatherInfo info = mWeatherData.get(groupPosition);
		return info.getForecastConditions().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.weather_item, null);
			holder = new ViewHolder();
			holder.txtTitle = (TextView)convertView.findViewById(R.id.txtTitle);
			holder.image = (ImageView)convertView.findViewById(R.id.image);
			holder.txtInfo = (TextView)convertView.findViewById(R.id.txtInfo);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		WeatherInfo info = mWeatherData.get(groupPosition);
		WeatherCondition condition = info.getForecastConditions().get(childPosition);
		holder.txtTitle.setText(condition.getDayOfWeek());
		holder.txtInfo.setText(condition.getCondition() + ", 温度：" + condition.getLow() + "° - " + condition.getHigh() + "°");
		//异步请求图片
		String icon = condition.getIcon();
		if (!"".equals(icon)) {
			AsyncImageRequest.getInstance().requestImage(mContext, AsyncImageRequest.FILE_AND_MEMORY_CACHE,
					WeatherXMLParserHandler.getImageURL(icon), holder.image, parent);
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mWeatherData.get(groupPosition).getForecastConditions().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mWeatherData.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return mWeatherData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.weather_item, null);
			holder = new ViewHolder();
			holder.txtTitle = (TextView)convertView.findViewById(R.id.txtTitle);
			holder.image = (ImageView)convertView.findViewById(R.id.image);
			holder.txtInfo = (TextView)convertView.findViewById(R.id.txtInfo);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		WeatherInfo info = mWeatherData.get(groupPosition);
		holder.txtTitle.setText(info.getPostalCode());
		holder.txtInfo.setText(info.getCurrentCondition().getCondition() + ", 温度：" + info.getCurrentCondition().getTempC() + "°, "
				+ info.getCurrentCondition().getHumidity() + ", "
				+ info.getCurrentCondition().getWindCondition());
		//异步请求图片
		String icon = info.getCurrentCondition().getIcon();
		if (!"".equals(icon)) {
			AsyncImageRequest.getInstance().requestImage(mContext, AsyncImageRequest.FILE_AND_MEMORY_CACHE,
					WeatherXMLParserHandler.getImageURL(icon), holder.image, parent);
		}

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}
	
	class ViewHolder {
    	ImageView image;
        TextView txtTitle;
        TextView txtInfo;
    }
}
